package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.entity.Groups;
import com.ayf.payment.game.api.entity.Merchant;
import com.ayf.payment.game.api.entity.MerchantRolePermission;
import com.ayf.payment.game.api.entity.MerchantSon;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.MerchantMapper;
import com.ayf.payment.game.api.mapper.MerchantRolePermissionMapper;
import com.ayf.payment.game.api.mapper.MerchantSonMapper;
import com.ayf.payment.game.api.service.GroupsService;
import com.ayf.payment.game.api.service.MerchantSonService;
import com.ayf.payment.game.api.type.Status;
import com.ayf.payment.game.api.utils.MD5Util;
import com.ayf.payment.game.api.utils.RandomStrUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MerchantSonServiceImpl implements MerchantSonService {

    @Resource
    private MerchantSonMapper merchantSonMapper;
    @Resource
    private MerchantMapper merchantMapper;
    @Resource
    private MerchantRolePermissionMapper merchantRolePermissionMapper;

    @Autowired
    private GroupsService groupsService;

    @Override
    public List<MerchantSon> list(MerchantSon merchantSon) {
        return merchantSonMapper.list(merchantSon);
    }

    @Override
    public List<MerchantSon> tuiGList(Integer merchantId) {
        return merchantSonMapper.tuiGList(merchantId);
    }

    @Override
    public MerchantSon selectInfo(Integer sonId) {
        return merchantSonMapper.selectByPrimaryKey(sonId);
    }

    @Override
    public boolean addSon(MerchantSon merchantSon) {
        if (merchantSon.getMerchantId() == null) {
            throw new ParameterException("主商户id不能为空");
        }
        if (merchantSon.getLoginAccount() == null) {
            throw new ParameterException("登录账号不能为空");
        }
        if (merchantSon.getRoleId() == null) {
            throw new ParameterException("必须分配一个角色");
        }
        if (merchantSon.getPassword() == null) {
            throw new ParameterException("登录密码不能为空");
        }
        if (merchantSon.getRepeatPassword() == null) {
            throw new ParameterException("确认密码不能为空");
        }
        if (!merchantSon.getRepeatPassword().equals(merchantSon.getPassword())) {
            throw new ParameterException("登录密码和确认密码不一样");
        }
        Merchant temp = new Merchant();
        temp.setLoginAccount(merchantSon.getLoginAccount());
        MerchantSon tempMerchantSon = new MerchantSon();
        tempMerchantSon.setLoginAccount(merchantSon.getLoginAccount());
        if (merchantMapper.selectOne(temp) != null || merchantSonMapper.selectOne(tempMerchantSon) != null) {
            throw new ParameterException("登录名称重复");
        }
        String random = RandomStrUtils.createRandom(false, 8);
        merchantSon.setSalt(random);
        merchantSon.setPassword(MD5Util.sign(merchantSon.getPassword(), random));
        merchantSon.setStatus(1);
        Integer roleId = merchantSon.getRoleId();
        MerchantRolePermission merchantRolePermission=new MerchantRolePermission();
        merchantRolePermission.setRoleId(roleId);
        List<MerchantRolePermission> select = merchantRolePermissionMapper.select(merchantRolePermission);
        if (CollectionUtils.isEmpty(select)) {
            throw new ParameterException("请先给角色进行授权");
        }
        merchantSonMapper.insertSelective(merchantSon);

        if (StringUtils.isNotBlank(merchantSon.getCode()) && merchantSon.getCode().equals("TuiG")) {
            Groups groups = new Groups();
            groups.setMerchantId(merchantSon.getMerchantId());
            groups.setGroupStatus(Status.ENABLE);
            groups.setGroupName("分组" + merchantSon.getId());
            groupsService.addTuiGroup(groups);
            merchantSon.setGroupId(groups.getId());
            merchantSonMapper.updateByPrimaryKeySelective(merchantSon);
        }
        return true;
    }

    @Override
    public boolean deleteSon(MerchantSon merchantSon) {
        if (merchantSon.getId() == null) {
            throw new ParameterException("子账号id不能为空");
        }
        return merchantSonMapper.deleteByPrimaryKey(merchantSon.getId()) > 0;
    }

    @Override
    public boolean updateSon(MerchantSon merchantSon) {
        if (merchantSon.getId() == null) {
            throw new ParameterException("子账号id不能为空");
        }
        return merchantSonMapper.updateByPrimaryKeySelective(merchantSon) > 0;
    }

    @Override
    public boolean updateSonPassword(MerchantSon merchantSon) {
        if (merchantSon.getId() == null) {
            throw new ParameterException("子账号id不能为空");
        }
        if (merchantSon.getPassword() == null) {
            throw new ParameterException("登录密码不能为空");
        }
        if (merchantSon.getRepeatPassword() == null) {
            throw new ParameterException("确认密码不能为空");
        }
        if (!merchantSon.getRepeatPassword().equals(merchantSon.getPassword())) {
            throw new ParameterException("登录密码和确认密码不一样");
        }
        String random = RandomStrUtils.createRandom(false, 8);
        merchantSon.setSalt(random);
        merchantSon.setPassword(MD5Util.sign(merchantSon.getPassword(), random));
        return merchantSonMapper.updateByPrimaryKeySelective(merchantSon) > 0;
    }
}
